package reserve

import (
    "time"
	"strconv"
	"dhfshop/controllers/base"
	"dhfshop/models/reserve"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/validation"
)

type TimeslotSTLController struct {
	base.BaseController
}

func (this *TimeslotSTLController) GroupList() {
	query,_,_,order_by,_,_,err := this.GetQueryPara()
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	partnerId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
	query["partner_id"] = strconv.FormatInt(partnerId, 10)
	if _, ok := query["schema_id"]; !ok {
		query["schema_id"] = "0"
	}
	if _, ok := query["period_stl_id"]; !ok {
		query["period_stl_id"] = "0"
	}
	list, err := reserve.GetAllTimeslotSTLs(query, order_by)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	rsp := struct {
		PeriodSTLs  interface{} `json:"timeslot_stls"`
	}{PeriodSTLs: list}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *TimeslotSTLController) GroupPost() {
	v := struct {
		PartnerId   int64      `form:"-"`
		SchemaId    int64    `form:"schema_id"`
		PeriodSTLId int64    `form:"period_stl_id"`
	}{}
	v.PartnerId = int64(this.Ctx.Input.GetData("curCompanyId").(int))
	this.ParseForm(&v)
	valid := validation.Validation{}
	//valid.Required(v.SchemaId, "schema_id")
	//valid.Required(v.PeriodSTLId, "period_stl_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	query := make(map[string]string)
	order_by := make(map[string]string)
	query["partner_id"] = strconv.FormatInt(v.PartnerId, 10)
	query["schema_id"] = "0"
	query["period_stl_id"] = strconv.FormatInt(v.PeriodSTLId, 10)
	order_by["timeslot_no"] = "asc"
	srcLists, err := reserve.GetAllTimeslotSTLs(query, order_by)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	if len(srcLists) < 1 {
	    beego.Debug("srcLists length:", len(srcLists))
		this.ServeError("src_lists lenght is invalid")
		return
	}
	dstLists := reserve.CopyTimeslotSTL(v.PartnerId, v.SchemaId, v.PeriodSTLId, srcLists)
	err = reserve.AddMultiTimeslotSTLs(dstLists)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	this.Data["json"] = dstLists
	this.ServeJSON()
}

func (this *TimeslotSTLController) Post() {
	var v reserve.TimeslotStl
	valid := validation.Validation{}
	this.ParseForm(&v)
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return	
	}
	v.PartnerId = int64(this.Ctx.Input.GetData("curCompanyId").(int))
	if _, err := reserve.AddTimeslotSTL(&v); err != nil {
        this.ServeError(err.Error())
		return
    }
	rsp := struct {
		TimeslotSTLId int64      `json:"timeslot_stl_id"`
	    Created       time.Time  `json:"created"`
	}{TimeslotSTLId: v.TimeslotSTLId, Created:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *TimeslotSTLController) Update() {
	var v reserve.TimeslotStl
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.TimeslotSTLId, "timeslot_stl_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	v.PartnerId = int64(this.Ctx.Input.GetData("curCompanyId").(int))
	updateFields, err := this.GetUpdateFields(v, "", "form", []string{"print_pub_id"})
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	if len(updateFields) > 0 {
		err := reserve.UpdateTimeslotSTLById(&v, updateFields...)
		if err != nil {
			this.ServeError(err.Error())
			return
		}
	}
	rsp := struct {
		TimeslotSTLId  int64     `json:"timeslot_stl_id"`
		Modified       time.Time `json:"modified"`
	}{TimeslotSTLId: v.TimeslotSTLId, Modified: time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *TimeslotSTLController) Delete() {
	var v reserve.TimeslotStl
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.TimeslotSTLId, "timeslot_stl_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	err := reserve.DeleteTimeslotSTLById(v.TimeslotSTLId)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	r := struct {
		TimeslotSTLId  int64       `json:"timeslot_stl_id"`
		Modified       time.Time   `json:"modified"`
	}{TimeslotSTLId: v.TimeslotSTLId, Modified: time.Now().UTC()}
	this.Data["json"] = r
	this.ServeJSON()
}

